package com.hmyd.hmyd_android.adapter;

import java.util.List;

import com.hmyd.hmyd_android.R;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hmyd.hmyd_android.dto.Coupon;
import com.hmyd.hmyd_android.utils.Utils;

public class CouponListAdapter extends BaseAdapter{
	
	private Context mContext;
	private List<Coupon> mPlist;
	private LayoutInflater mInflater;
	private String mType;

	public CouponListAdapter(Context context, List<Coupon> plist, String type){
		super();
		
		this.mContext = context;
		this.mPlist = plist;
		mInflater=LayoutInflater.from(context);
		mType = type;
	}
	
	@Override
	public int getCount() {
		return mPlist != null ? mPlist.size() : 0;
	}

	@Override
	public Coupon getItem(int position) {
		return mPlist != null ? mPlist.get(position) : null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(mPlist==null || mPlist.get(position)==null){
			return convertView;
		}
		
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_coupon_list_type_item, null);
			holder = new ViewHolder();
			holder.nominalValue = (TextView)convertView.findViewById(R.id.nominalValue);
			holder.channelTitle = (TextView)convertView.findViewById(R.id.channelTitle);
			holder.time = (TextView)convertView.findViewById(R.id.time);
			holder.tip = (TextView)convertView.findViewById(R.id.tip);
			holder.applyToItemType = (TextView)convertView.findViewById(R.id.applyToItemType);
			
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		Coupon coupon = getItem(position);
		if(coupon!=null){
			holder.nominalValue.setText(Utils.amountConversionFormat(coupon.nominalValue));
			holder.channelTitle.setText(coupon.channelTitle);
			
			// 判断 nominalValue 长度显示字体大小
			int length = holder.nominalValue.length();
			if (length >= 4) {
				holder.nominalValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
			} else if (length >= 3) {
				holder.nominalValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
			} else {
				holder.nominalValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 45);
			}
			
			if("usable".equals(mType)){//可使用
				holder.time.setText("有效期："+Utils.getDateString(coupon.effectTime)+"至"+Utils.getDateString(coupon.expTime));
				holder.tip.setText("所需投资金额："+Utils.amountConversionFormat(coupon.requiredMinInvestValue)+"元");
				
			}else if("used".equals(mType)){//已经使用
				holder.time.setText("使用时间："+Utils.getDateString(coupon.usedTime));
				holder.tip.setText("投资项目："+coupon.itemName);
				
			}else if("expired".equals(mType)){//过期
				holder.time.setText("过期时间："+Utils.getDateString(coupon.expTime));
				holder.tip.setText("所需投资金额："+Utils.amountConversionFormat(coupon.requiredMinInvestValue)+"元");
				convertView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.zi_img_xianguo));
			}
			
			holder.applyToItemType.setText("使用限制："+coupon.applyToItemType);
			
		}
		
		return convertView;
	}
	
	class ViewHolder{
		public TextView nominalValue;
		public TextView channelTitle;
		public TextView time;
		public TextView tip;
		public TextView applyToItemType;
	}
	
}
